package com.talkology.pilot.common.util;


import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @ClassName: SendEmail
 * @Description: 发送Email
 * @author: longJp
 * @date: 2015-1-12 下午9:42:56
 */
public class SendEmail {

    private static final String mailFrom = "wz@talkology.cn";
    private static final String password = "WrightLin8255124";

    public static void main(String[] args) throws MessagingException {
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");// 连接协议
        properties.put("mail.smtp.host", "smtp.exmail.qq.com");// 主机名
        properties.put("mail.smtp.port", 25);// 端口号
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "false");// 设置是否使用ssl安全连接 ---一般都使用
        properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
        // 得到回话对象
        Session session = Session.getInstance(properties);
        // 获取邮件对象
        Message message = new MimeMessage(session);
        // 设置发件人邮箱地址
        message.setFrom(new InternetAddress(mailFrom));
        // 设置收件人邮箱地址
        message.setRecipient(Message.RecipientType.TO, new InternetAddress("long_jpeng@163.com"));//一个收件人
        // 设置邮件标题
        message.setSubject("xmqtest");
        // 设置邮件内容
        message.setText("邮件内容邮件内容邮件内容xmqtest");
        // 得到邮差对象
        Transport transport = session.getTransport();
        // 连接自己的邮箱账户
        transport.connect(mailFrom, password);// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
        // 发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    /**
     * 一封邮件发给一个收件箱
     * @param mail 收件箱地址
     * @param subject 邮件主题
     * @param text 邮件内容
     * @throws MessagingException 异常
     */
    public static void sendEmail(String mail, String subject, String text) throws MessagingException {
        Session session = Session.getInstance(getProperties());
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(mailFrom));
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(mail));
        message.setSubject(subject);
        message.setText(text);
        Transport transport = session.getTransport();
        transport.connect(mailFrom, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    /**
     * 一封邮件发给多个收件箱
     * @param mails 收件箱地址
     * @param subject 邮件主题
     * @param text 邮件内容
     * @throws MessagingException 异常
     */
    public static void sendEmails(String[] mails, String subject, String text) throws MessagingException {
        Session session = Session.getInstance(getProperties());
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(mailFrom));
        InternetAddress[] mailToArr = new InternetAddress[mails.length];
        for (int i = 0; i< mails.length; i ++) {
            mailToArr[i] = new InternetAddress(mails[i]);
        }
        message.setRecipients(Message.RecipientType.TO, mailToArr);
        message.setSubject(subject);
        message.setText(text);
        Transport transport = session.getTransport();
        transport.connect(mailFrom, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    private static Properties getProperties(){
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.host", "smtp.exmail.qq.com");
        properties.put("mail.smtp.port", 25);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "false");
        properties.put("mail.debug", "true");
        return properties;
    }
}